#pragma once
#include <fstream>
#include <vector>
#include <assert.h>
#include <iostream> 
#include "file_utility.hpp"
#include "gpu_utility.hpp"


static void encrypt_data(std::vector<char> data)
{
	char trans = 55;
	for (int i = 0; i < data.size(); ++i)
	{
		data[i] += trans;
		trans += 11;
	}
}

static void encryptFile(st_encrypt_info info, std::vector<char> engine_data, std::string target_path)
{
	info.engine_size = engine_data.size();

	std::vector<char> target_data(sizeof(info), 0);
	*((st_encrypt_info*)(target_data.data())) = info;

	target_data.insert(target_data.end(), engine_data.begin(), engine_data.end());

	encrypt_data(target_data);
	write_file_data(target_data, target_path);
	std::cout << "writing encrypt file..." << target_path.c_str() << " done!" << std::endl;

}

static void decrypt_data(std::vector<char> data)
{
	char trans = 55;
	for (int i = 0; i < data.size(); ++i)
	{
		data[i] -= trans;
		trans += 11;
	}
}

static bool decryptFile(std::string file_path, st_encrypt_info& info, std::vector<char>& engine_data)
{
	std::vector<char> file_data = get_file_data(file_path);
	decrypt_data(file_data);
	if (file_data.size() <= sizeof(st_encrypt_info))
	{
		std::cout << "file data too small!\n";
		return false;
	}
	info = *((st_encrypt_info*)file_data.data());
	if (info.engine_size + sizeof(st_encrypt_info) != file_data.size())
	{
		std::cout << "file data is bad format!\n";
		return false;
	}

	std::string gpu_model = GetGPUModel();
	std::string build_gpu_model = info.gpu_model;
	if (gpu_model != build_gpu_model)
	{
		std::cout << "gpu model warning: build is: " << build_gpu_model.c_str() << " target is: " << gpu_model.c_str() << std::endl;
	}

	int index = sizeof(st_encrypt_info);
	engine_data.resize(info.engine_size);
	memcpy(engine_data.data(), file_data.data() + index, info.engine_size);
	return true;
}

